<?php
namespace app\api\logic;


use app\common\lib\SendIntegralNew;
use think\Controller;

class Task extends Controller
{
    //获取用户奖扣任务详情
    static public function get_prize_buckle_task($uid,$time){
        $now_time = time();
        $sys = M('com_prize_buckle_config_relation_users a')
            ->join('com_prize_buckle_config b','a.prize_buckle_id=b.id','left')
            ->where('a.user_id', $uid)
            ->field('b.*')
            ->find();
        $start = strtotime($time.'-01');
        $end = strtotime('+1 month',$start)-1;
        //统计
        //判断奖扣分任务类型
        $res = SendIntegralNew::statistic_prize_buckle($start,$end,$uid);
        $total_prize = $res['integral_b_add'];
        $total_buckle = abs($res['integral_b_del']);
        //总人次
        $total_people = $res['people_sum'];
        //比例
        $rate_status = 1;
        if($res['integral_b_add'] == 0){
            $total_rate = 0;
        }else{
            $total_rate = round(abs($res['integral_b_del'])/$res['integral_b_add']*100,2);
        }
        if($total_rate < $sys['rate']){
            $rate_status = 0;
            $rate_del = ceil(($sys['rate']*$res['integral_b_add']/100 - abs($res['integral_b_del'])));
        }else{
            $rate_del = 0;
        }
        $total_rate = round($total_rate);
        //奖扣
        if ($sys['integral_type'] == 1) { //月任务
            //月任务
            //统计

            //总奖分
            $total_prize = $res['integral_b_add'];
            //总奖分任务状态
            if($total_prize < $sys['integral_reward']){
                $total_prize_status = 0;
                $total_prize_del = $sys['integral_reward'] - $total_prize;
            }else{
                $total_prize_status = 1;
                $total_prize_del = 0;
            }
            $total_buckle = abs($res['integral_b_del']);
            //总扣分任务状态
            if($total_buckle < $sys['integral_deduct']){
                $total_buckle_status = 0;
                $total_buckle_del = $sys['integral_deduct'] - $total_buckle;
            }else{
                $total_buckle_status = 1;
                $total_buckle_del = 0;
            }

        } else {
            $total_prize_status = 1;
            $total_buckle_status = 1;
            //周任务
            //第一周
            $week_1_start = $start;
            $week_1_end = $start + 3600 * 24 * 7;
            $res_week_1 = SendIntegralNew::statistic_prize_buckle($week_1_start,$week_1_end,$uid);
            //奖任务扣分
            if($res_week_1['integral_b_add'] < $sys['integral_reward']){
                $total_prize_status = 0;
                $total_prize_1_del = $sys['integral_reward'] - $res_week_1['integral_b_add'];
            }else{
                $total_prize_1_del = 0;
            }
            //扣任务扣分
            if(abs($res_week_1['integral_b_del']) < $sys['integral_deduct']){
                $total_buckle_status = 0;
                $total_buckle_1_del = $sys['integral_deduct'] - abs($res_week_1['integral_b_del']);
            }else{
                $total_buckle_1_del = 0;
            }

            //第二周
            $week_2_start = $week_1_end;
            $week_2_end = $week_1_end + 3600 * 24 * 7;
            $res_week_2 = SendIntegralNew::statistic_prize_buckle($week_2_start,$week_2_end,$uid);
            //奖任务扣分
            if($res_week_2['integral_b_add'] < $sys['integral_reward']){
                $total_prize_status = 0;
                $total_prize_2_del = $sys['integral_reward'] - $res_week_2['integral_b_add'];
            }else{
                $total_prize_2_del = 0;
            }
            //扣任务扣分
            if(abs($res_week_2['integral_b_del']) < $sys['integral_deduct']){
                $total_buckle_status = 0;
                $total_buckle_2_del = $sys['integral_deduct'] - abs($res_week_2['integral_b_del']);
            }else{
                $total_buckle_2_del = 0;
            }
            //第三周
            $week_3_start = $week_2_end;
            $week_3_end = $week_2_end + 3600 * 24 * 7;
            $res_week_3 = SendIntegralNew::statistic_prize_buckle($week_3_start,$week_3_end,$uid);
            //奖任务扣分
            if($res_week_3['integral_b_add'] < $sys['integral_reward']){
                $total_prize_status = 0;
                $total_prize_3_del = $sys['integral_reward'] - $res_week_3['integral_b_add'];
            }else{
                $total_prize_3_del = 0;
            }
            //扣任务扣分
            if(abs($res_week_3['integral_b_del']) < $sys['integral_deduct']){
                $total_buckle_status = 0;
                $total_buckle_3_del = $sys['integral_deduct'] - abs($res_week_3['integral_b_del']);
            }else{
                $total_buckle_3_del = 0;
            }
            //第四周
            $week_4_start = $week_3_end;
            $week_4_end = $end;
            $res_week_4 = SendIntegralNew::statistic_prize_buckle($week_4_start,$week_4_end,$uid);
            //奖任务扣分
            if($res_week_4['integral_b_add'] < $sys['integral_reward']){
                $total_prize_status = 0;
                $total_prize_4_del = $sys['integral_reward'] - $res_week_4['integral_b_add'];
            }else{
                $total_prize_4_del = 0;
            }
            //扣任务扣分
            if(abs($res_week_4['integral_b_del']) < $sys['integral_deduct']){
                $total_buckle_status = 0;
                $total_buckle_4_del = $sys['integral_deduct'] - abs($res_week_4['integral_b_del']);
            }else{
                $total_buckle_4_del = 0;
            }
            //总奖分扣分
            $total_prize_del = $total_prize_1_del + $total_prize_2_del +$total_prize_3_del +$total_prize_4_del ;
            //总扣分扣分
            $total_buckle_del = $total_buckle_1_del + $total_buckle_2_del +$total_buckle_3_del +$total_buckle_4_del ;
        }
        //人次
        $people_com_count = 0;//人次完成次数
        $where['first_check_user_id'] = $uid; //初审人
        $where['prize_buckle_time'] = ['between', [$start, $end]]; //奖扣时间范围
        $where['status'] = 2; //审核通过
        $res_people = M('com_integral_prize_buckle')
            ->where($where)
            ->select();
        foreach ($res_people as $key=>$value){
            $res_people[$key]['day'] = date('d',$value['prize_buckle_time']);
        }
        $people_status = 1;
        $people_status_now = 1;
        $people_sum_now = 0;
        $max = (int)date('d',$end);
        $day_off_sum = 0; //为完成天数
        for ($i=1;$i<=$max;$i++){
            $people_sum = 0;
            foreach ($res_people as $v){
                if($v['day'] == $i){
                    $people_sum += $v['people_sum'];
                }
            }
            if($people_sum < $sys['man_num']){
                $day_off_sum ++;

            }else{
                if($sys['man_type'] == 1){
                    $people_com_count++;
                }
            }
        }
        //已完成人次
        $total_people_a = 0;
        if($sys['man_type'] == 1){ //日任务
            //统计为完成天数
            if($day_off_sum > 0){
                $people_status = 0;
                $people_del_total = $sys['man_deduct']*$day_off_sum;
            }else{
                //扣分
                $people_del_total = 0;
            }
            $time_start = strtotime(date('Y-m-d',$now_time));
            $time_end = strtotime('+1 day',$time_start)-1;
            $res = SendIntegralNew::statistic_prize_buckle($time_start,$time_end,$uid);
            $total_people_a = $res['people_sum'];
            $people_sum_now = $res['people_sum'];
            if($sys['man_num'] >$res['people_sum']){
                $people_status_now = 0;
            }


        }elseif($sys['man_type'] == 2){//人次周任务
            //第一周
            $week_1_people_start = $start;
            $week_1_people_end = $start + 3600 * 24 * 7;
            $res_week_1_people = SendIntegralNew::statistic_prize_buckle($week_1_people_start,$week_1_people_end,$uid);
            if($week_1_people_start<=$now_time && $week_1_people_end>=$now_time){
                $total_people_a = $res_week_1_people['people_sum'];
                $people_sum_now = $res_week_1_people['people_sum'];
                if($sys['man_num'] >$res_week_1_people['people_sum']){
                    $people_status_now = 0;
                }
            }
            if($res_week_1_people['people_sum'] < $sys['man_num']){
                $people_status = 0;
                $people_del_1 = $sys['man_deduct'];
            }else{
                $people_del_1 = 0;
                $people_com_count++;
            }
            //第二周
            $week_2_people_start = $week_1_people_end;
            $week_2_people_end = $week_1_people_end + 3600 * 24 * 7;
            $res_week_2_people = SendIntegralNew::statistic_prize_buckle($week_2_people_start,$week_2_people_end,$uid);
            if($week_2_people_start<=$now_time && $week_2_people_end>=$now_time){
                $total_people_a = $res_week_2_people['people_sum'];
                $people_sum_now = $res_week_2_people['people_sum'];
                if($sys['man_num'] >$res_week_2_people['people_sum']){
                    $people_status_now = 0;
                }
            }
            if($res_week_2_people['people_sum'] < $sys['man_num']){
                $people_status = 0;
                $people_del_2 = $sys['man_deduct'];
            }else{
                $people_com_count++;
                $people_del_2 = 0;
            }
            //第三周
            $week_3_people_start = $week_2_people_end;
            $week_3_people_end = $week_2_people_end + 3600 * 24 * 7;
            $res_week_3_people = SendIntegralNew::statistic_prize_buckle($week_3_people_start,$week_3_people_end,$uid);
            if($week_3_people_start<=$now_time && $week_3_people_end>=$now_time){
                $total_people_a = $res_week_3_people['people_sum'];
                $people_sum_now = $res_week_3_people['people_sum'];
                if($sys['man_num'] >$res_week_3_people['people_sum']){
                    $people_status_now = 0;
                }
            }
            if($res_week_3_people['people_sum'] < $sys['man_num']){
                $people_status = 0;
                $people_del_3 = $sys['man_deduct'];
            }else{
                $people_com_count++;
                $people_del_3 = 0;
            }
            //第四周
            $week_4_people_start = $week_3_people_end;
            $week_4_people_end = $end;
            $res_week_4_people = SendIntegralNew::statistic_prize_buckle($week_4_people_start,$week_4_people_end,$uid);
            if($week_4_people_start<=$now_time && $week_4_people_end>=$now_time){
                $total_people_a = $res_week_4_people['people_sum'];
                $people_sum_now = $res_week_4_people['people_sum'];
                if($sys['man_num'] >$res_week_4_people['people_sum']){
                    $people_status_now = 0;
                }
            }
            if($res_week_4_people['people_sum'] < $sys['man_num']){
                $people_status = 0;
                $people_del_4 = $sys['man_deduct'];
            }else{
                $people_com_count++;
                $people_del_4 = 0;
            }
            $people_del_total = $people_del_1 + $people_del_2 + $people_del_3 + $people_del_4;
        }else{//人次月任务
            //月人次
            $where_month_4_people['first_check_user_id'] = $uid; //初审人
            $where_month_4_people['prize_buckle_time'] = ['between', [$start, $end]]; //奖扣时间范围
            $where_month_4_people['status'] = 2; //审核通过
            $res_month_people = SendIntegralNew::statistic_prize_buckle($start,$end,$uid);
            $total_people_a = $res_month_people['people_sum'];
            $people_sum_now = $res_month_people['people_sum'];
            if($sys['man_num'] >$res_month_people['people_sum']){
                $people_status_now = 0;
            }
            if($res_month_people['people_sum'] < $sys['man_num']){
                $people_status = 0;
                $people_del_total = $sys['man_deduct'];
            }else{
                $people_com_count++;
                $people_del_total = 0;
            }
        }

        if($sys['man_num']>$total_people_a){
            $people_del = $sys['man_deduct'];
        }else{
            $people_del = 0;
        }
        $total_del_integral = $total_prize_del + $total_buckle_del + $rate_del + $people_del_total;
        $data = [
            'prize_integral_target'=>$sys['integral_reward'],//奖分目标
            'buckle_integral_target'=>$sys['integral_deduct'],//扣分目标
            'prize_integral_com'=>$total_prize,//奖分完成值
            'buckle_integral_com'=>$total_buckle,//扣分完成值
            'prize_integral_status'=>$total_prize_status,//奖分状态  0未完成  1已完成
            'buckle_integral_status'=>$total_buckle_status,//扣分状态  0未完成 1已完成
            'prize_integral_del'=>$total_prize_del,//奖分未完成扣分
            'buckle_integral_del'=>$total_buckle_del,//扣分未完成扣分
            'rate_target'=>$sys['rate'],//奖扣比例目标
            'rate_com'=>$total_rate,//奖扣比例已完成值
            'rate_status'=>$rate_status,//完成状态 0未完成 1已完成
            'rate_del'=>$rate_del,//比例未完成扣分
            'people_day'=>$max,//人次月天数
            'people_day_total'=>(int)date('d',time()),//人次统计天数
            'people_day_not'=>$day_off_sum,//人次未完成天数
            'people_target'=>$sys['man_num'],//奖扣人次目标
            'people_com'=>$total_people_a,//奖扣人次已完成值
            'people_del'=>$people_del,//人次未完成扣分
            'people_del_total'=>$people_del_total,//人次未完成总扣分
            'people_status'=>$people_status,//人次状态  0未完成 1已完成
            'people_sum_now'=>$people_sum_now,//总人次 @新增
            'people_status_now'=>$people_status_now,//人次状态  0未完成 1已完成 @新增
            'prize_buckle_type'=>$sys['integral_type'],//奖扣任务类型 0周任务  1月任务
            'people_type'=>$sys['man_type'],//奖扣人次类型  1日任务  2周任务  3月任务
            'total_people'=>$total_people,//总人次
            'total_del_integral'=>$total_del_integral,//总扣分
            'prize_buckle_task_name'=>$sys['name'],//奖扣任务名称
            'people_com_count'=>$people_com_count,//人次完成次数
        ];
        return $data;
    }
    //获取奖扣任务昨天,本月奖分,扣分,奖扣人次
    static public function get_prize_buckle_task_state($uid){
        $time = strtotime('Y-m-d',time());
        //昨天时间
        $start_time = strtotime(date('Y-m-d',strtotime('-1 day',$time)));
        $end_time = $start_time+ 24*3600;
        //
        $where['a.first_check_user_id'] = $uid; //初审人
        $where['a.prize_buckle_time'] = ['between', [$start_time, $end_time]]; //奖扣时间范围
        $where['a.status'] = 2; //审核通过
        $where['c.is_cancellation'] = 0;//未作废
        $res = M('com_integral_prize_buckle a')
            ->join('com_integral_prize_buckle_relation_event b','a.id=b.prize_buckle_id','right')
            ->join('com_integral_event_relation_users c','c.event_id=b.id','right')
            ->where($where)
            ->field('
            COALESCE(SUM(IF(c.integral_b>0,IF(c.piece_rate=0,c.integral_b,c.piece_rate*c.integral_b),0)),0) integral_b_add, 
            COALESCE(SUM(IF(c.integral_b<0,IF(c.piece_rate=0,c.integral_b,c.piece_rate*c.integral_b),0)),0) integral_b_del, 
            count(c.id) people_sum
            ')
            ->find();
        //本月时间
        $start_time_month = strtotime(date('Y-m-01',$time));
        $end_time_month = strtotime('+1 month',$start_time_month)-1;
        $where['a.prize_buckle_time'] = ['between', [$start_time_month, $end_time_month]]; //奖扣时间范围
        $res_month= M('com_integral_prize_buckle a')
            ->join('com_integral_prize_buckle_relation_event b','a.id=b.prize_buckle_id','right')
            ->join('com_integral_event_relation_users c','c.event_id=b.id','right')
            ->where($where)
            ->field('
            COALESCE(SUM(IF(c.integral_b>0,IF(c.piece_rate=0,c.integral_b,c.piece_rate*c.integral_b),0)),0) integral_b_add, 
            COALESCE(SUM(IF(c.integral_b<0,IF(c.piece_rate=0,c.integral_b,c.piece_rate*c.integral_b),0)),0) integral_b_del, 
            count(c.id) people_sum
            ')
            ->find();
        $data = [
            'yesterday_prize_integral'=>$res['integral_b_add'],
            'yesterday_buckle_integral'=>$res['integral_b_del'],
            'yesterday_peopel_sum'=>$res['people_sum'],
            'month_prize_integral'=>$res_month['integral_b_add'],
            'month_buckle_integral'=>$res_month['integral_b_del'],
            'month_peopel_sum'=>$res_month['people_sum'],
        ];
        return $data;

    }
    //人次任务
    static public function get_people_task($uid,$time){
        $start = strtotime($time.'-01');
        $end = strtotime('+1 month -1 day',$start);
        $sys = M('com_prize_buckle_config_relation_users a')
            ->join('com_prize_buckle_config b','a.prize_buckle_id=b.id','left')
            ->where('a.user_id', $uid)
            ->field('b.*')
            ->find();
        //人次
        $where['c.first_check_user_id'] = $uid; //初审人
        $where['c.prize_buckle_time'] = ['between', [$start, $end]]; //奖扣时间范围
        $where['c.status'] = 2; //审核通过
        $where['a.is_cancellation'] = 0;//未作废
        $res_people = M('com_integral_event_relation_users a')
            ->join('com_integral_prize_buckle_relation_event b','b.id=a.event_id','left')
            ->join('com_integral_prize_buckle c','c.id=b.prize_buckle_id','left')
            ->join('users d','d.id=a.user_id','left')
            ->join('com_event e','e.id=b.event_id','left')
            ->where($where)
            ->order('c.prize_buckle_time desc')
            ->field('c.prize_buckle_time,d.name,e.name event_name,a.integral_b,c.people_sum')
            ->select();
        foreach ($res_people as $key=>$value){
            $res_people[$key]['day'] = date('d',$value['prize_buckle_time']);
            $res_people[$key]['prize_buckle_time'] = date('m-d',$value['prize_buckle_time']);
        }
        $data = [];
        $data_total = [];
//        $sys['man_type'] = 1;
        if($sys['man_type'] == 1){ //日任务
            $max = (int)date('d',$end);
            for ($i=1;$i<=$max;$i++){
                $people_sum = 0;
                foreach ($res_people as $v){
                    if($v['day'] == $i){
                        $people_sum ++;
                    }
                }
                $data[]=[
                    'time'=>$i,
                    'people_sum'=>$people_sum,
                    'data_day'=>$sys['man_num'],
                ];
            }
            $data_total['type'] = 1;
        }elseif($sys['man_type'] == 2){//人次周任务
            //第一周
            $week_1_people_start = $start;
            $week_1_people_end = $start + 3600 * 24 * 7;
            $res_week_1_people = SendIntegralNew::statistic_prize_buckle($week_1_people_start,$week_1_people_end,$uid);
            $data_day = [];
            foreach ($res_people as $key=>$value){
                if($value['day'] < 8){
                    $data_day[] = $value;
                }
            }
            $data[]=[
                'time'=>1,
                'people_sum'=>$res_week_1_people['people_sum'],
                'data_day'=>$sys['man_num'],
            ];
            //第二周
            $week_2_people_start = $week_1_people_end;
            $week_2_people_end = $week_1_people_end + 3600 * 24 * 7;
            $res_week_2_people = SendIntegralNew::statistic_prize_buckle($week_2_people_start,$week_2_people_end,$uid);
            $data_day = [];
            foreach ($res_people as $key=>$value){
                if($value['day'] >= 8 && $value['day'] <= 14){
                    $data_day[] = $value;
                }
            }
            $data[]=[
                'time'=>2,
                'people_sum'=>$res_week_2_people['people_sum'],
                'data_day'=>$sys['man_num'],
            ];
            //第三周
            $week_3_people_start = $week_2_people_end;
            $week_3_people_end = $week_2_people_end + 3600 * 24 * 7;
            $res_week_3_people = SendIntegralNew::statistic_prize_buckle($week_3_people_start,$week_3_people_end,$uid);
            $data_day = [];
            foreach ($res_people as $key=>$value){
                if($value['day'] >= 15 && $value['day'] <= 21){
                    $data_day[] = $value;
                }
            }
            $data[]=[
                'time'=>3,
                'people_sum'=>$res_week_3_people['people_sum'],
                'data_day'=>$sys['man_num'],
            ];
            //第四周
            $week_4_people_start = $week_3_people_end;
            $week_4_people_end = $end;
            $res_week_4_people = SendIntegralNew::statistic_prize_buckle($week_4_people_start,$week_4_people_end,$uid);
            $data_day = [];
            foreach ($res_people as $key=>$value){
                if($value['day'] > 21){
                    $data_day[] = $value;
                }
            }
            $data[]=[
                'time'=>4,
                'people_sum'=>$res_week_4_people['people_sum'],
                'data_day'=>$sys['man_num'],
            ];
            $data_total['type'] = 2;
        }else{//人次月任务
            //月人次
            $res_month_people = SendIntegralNew::statistic_prize_buckle($start,$end,$uid);
            $data = [];
            if($res_month_people['people_sum'] == 0 || $sys['man_num'] == 0){
                $people_rate = 0;
            }else{
                if($res_month_people['people_sum']>$sys['man_num']){
                    $people_rate = 100;
                }else{
                    $people_rate = round($res_month_people['people_sum']/$sys['man_num'],3)*100;
                }

            }
            $data_total = [
                'total_prize'=>$res_month_people['integral_b_add'],
                'total_buckle'=>$res_month_people['integral_b_del'],
                'total_people'=>$res_month_people['people_sum'],
                'data_day'=>$sys['man_num'],
                'people_rate'=>$people_rate,
                'type'=>3
            ];
        }
        $data_total['people_target'] = $sys['man_num'];
        $data_a = [
            'list'=>$data,
            'data_total'=>$data_total
        ];
        return $data_a;
    }
    //奖扣任务-奖分和扣分
    static public function get_prize_buckle_integral_task($uid,$time){
        $sys = M('com_prize_buckle_config_relation_users a')
            ->join('com_prize_buckle_config b','a.prize_buckle_id=b.id','left')
            ->where('a.user_id', $uid)
            ->field('b.*')
            ->find();
        $start = strtotime($time.'-01');
        $end = strtotime('+1 month',$start)-1;
        //统计
        //判断奖扣分任务类型
        $res = SendIntegralNew::statistic_prize_buckle($start,$end,$uid);
        //奖扣
//        $sys['integral_type'] = 1;
        if ($sys['integral_type'] == 1) { //月任务
            //月任务
            //统计

            //总奖分
            $total_prize = $res['integral_b_add'];
            //总奖分任务状态
            if($total_prize < $sys['integral_reward']){
                $total_prize_status = 0;
                $total_prize_del = $sys['integral_reward'] - $total_prize;
            }else{
                $total_prize_status = 1;
                $total_prize_del = 0;
            }
            if($total_prize == 0 || $sys['integral_reward'] == 0){
                $prize_rate = 0;
            }else{
                if($total_prize > $sys['integral_reward']){
                    $prize_rate = 100;
                }else{
                    $prize_rate = round($total_prize/$sys['integral_reward'],3)*100;
                }
            }
            $total_buckle = abs($res['integral_b_del']);
            //总扣分任务状态
            if($total_buckle < $sys['integral_deduct']){
                $total_buckle_status = 0;
                $total_buckle_del = $sys['integral_deduct'] - $total_buckle;
            }else{
                $total_buckle_status = 1;
                $total_buckle_del = 0;
            }
            if($total_buckle == 0 || $sys['integral_deduct'] == 0){
                $buckle_rate = 0;
            }else{
                if($total_buckle > $sys['integral_deduct']){
                    $buckle_rate = 100;
                }else{
                    $buckle_rate = round($total_buckle/$sys['integral_deduct'],3)*100;
                }

            }
            $data = [
                'type'=>1,//类型
                'total_prize_del'=>$total_prize_del, //奖分总扣分
                'total_buckle_del'=>$total_buckle_del,//扣分总扣分
                'total_prize'=>$total_prize, //总奖分
                'total_buckle'=>$total_buckle, //总扣分
                'integral_reward'=>$sys['integral_reward'],//奖分目标
                'integral_deduct'=>$sys['integral_deduct'],//扣分目标
                'prize_rate'=>$prize_rate,//奖分完成比例
                'buckle_rate'=>$buckle_rate,//扣分完成比例
            ];
        } else {
            $total_prize_status = 1;
            $total_buckle_status = 1;
            $list_prize = [];
            $list_buckle = [];
            //周任务
            //第一周
            $week_1_start = $start;
            $week_1_end = $start + 3600 * 24 * 7;
            $res_week_1 = SendIntegralNew::statistic_prize_buckle($week_1_start,$week_1_end,$uid);
            //奖任务扣分
            if($res_week_1['integral_b_add'] < $sys['integral_reward']){
                $total_prize_status = 0;
                $total_prize_1_del = $sys['integral_reward'] - $res_week_1['integral_b_add'];
            }else{
                $total_prize_1_del = 0;
            }
            //扣任务扣分
            if(abs($res_week_1['integral_b_del']) < $sys['integral_deduct']){
                $total_buckle_status = 0;
                $total_buckle_1_del = $sys['integral_deduct'] - abs($res_week_1['integral_b_del']);
            }else{
                $total_buckle_1_del = 0;
            }
            $list_prize[] =[
                'time'=>1,
                'total_prize'=>abs($res_week_1['integral_b_add']), //总奖分
                'integral_reward' => $sys['integral_reward']
            ];
            $list_buckle[] =[
                 'time'=>1,
                 'total_buckle'=>abs($res_week_1['integral_b_del']),//总扣分
                 'integral_deduct' => $sys['integral_deduct'] //总扣分
            ];

            //第二周
            $week_2_start = $week_1_end+1;
            $week_2_end = $week_1_end + 3600 * 24 * 7;
            $res_week_2 = SendIntegralNew::statistic_prize_buckle($week_2_start,$week_2_end,$uid);
            //奖任务扣分
            if($res_week_2['integral_b_add'] < $sys['integral_reward']){
                $total_prize_status = 0;
                $total_prize_2_del = $sys['integral_reward'] - $res_week_2['integral_b_add'];
            }else{
                $total_prize_2_del = 0;
            }
            //扣任务扣分
            if(abs($res_week_2['integral_b_del']) < $sys['integral_deduct']){
                $total_buckle_status = 0;
                $total_buckle_2_del = $sys['integral_deduct'] - abs($res_week_2['integral_b_del']);
            }else{
                $total_buckle_2_del = 0;
            }
            $list_prize[] =[
                'time'=>2,
                'total_prize'=>abs($res_week_2['integral_b_add']), //总奖分
                'integral_reward' => $sys['integral_reward']
            ];
            $list_buckle[] =[
                'time'=>2,
                'total_buckle'=>abs($res_week_2['integral_b_del']),//总扣分
                'integral_deduct' => $sys['integral_deduct'] //总扣分
            ];
            //第三周
            $week_3_start = $week_2_end+1;
            $week_3_end = $week_2_end + 3600 * 24 * 7;
            $res_week_3 = SendIntegralNew::statistic_prize_buckle($week_3_start,$week_3_end,$uid);
            //奖任务扣分
            if($res_week_3['integral_b_add'] < $sys['integral_reward']){
                $total_prize_status = 0;
                $total_prize_3_del = $sys['integral_reward'] - $res_week_3['integral_b_add'];
            }else{
                $total_prize_3_del = 0;
            }
            //扣任务扣分
            if(abs($res_week_3['integral_b_del']) < $sys['integral_deduct']){
                $total_buckle_status = 0;
                $total_buckle_3_del = $sys['integral_deduct'] - abs($res_week_3['integral_b_del']);
            }else{
                $total_buckle_3_del = 0;
            }
            $list_prize[] =[
                'time'=>3,
                'total_prize'=>abs($res_week_3['integral_b_add']), //总奖分
                'integral_reward' => $sys['integral_reward']
            ];
            $list_buckle[] =[
                'time'=>3,
                'total_buckle'=>abs($res_week_3['integral_b_del']),//总扣分
                'integral_deduct' => $sys['integral_deduct'] //总扣分
            ];
            //第四周
            $week_4_start = $week_3_end+1;
            $week_4_end = $end;
            $res_week_4 = SendIntegralNew::statistic_prize_buckle($week_4_start,$week_4_end,$uid);
            //奖任务扣分
            if($res_week_4['integral_b_add'] < $sys['integral_reward']){
                $total_prize_status = 0;
                $total_prize_4_del = $sys['integral_reward'] - $res_week_4['integral_b_add'];
            }else{
                $total_prize_4_del = 0;
            }
            //扣任务扣分
            if(abs($res_week_4['integral_b_del']) < $sys['integral_deduct']){
                $total_buckle_status = 0;
                $total_buckle_4_del = $sys['integral_deduct'] - abs($res_week_4['integral_b_del']);
            }else{
                $total_buckle_4_del = 0;
            }
            $list_prize[] =[
                'time'=>4,
                'total_prize'=>abs($res_week_4['integral_b_add']), //总奖分
                'integral_reward' => $sys['integral_reward']
            ];
            $list_buckle[] =[
                'time'=>4,
                'total_buckle'=>abs($res_week_4['integral_b_del']),//总扣分
                'integral_deduct' => $sys['integral_deduct'] //总扣分
            ];
            //总奖分扣分
            $total_prize_del = $total_prize_1_del + $total_prize_2_del +$total_prize_3_del +$total_prize_4_del ;
            //总扣分扣分
            $total_buckle_del = $total_buckle_1_del + $total_buckle_2_del +$total_buckle_3_del +$total_buckle_4_del ;
            $data = [
                'type'=>0,//周任务
                'total_prize_del'=>$total_prize_del, //奖分总扣分
                'total_buckle_del'=>$total_buckle_del,//扣分总扣分
                'list_prize'=>$list_prize,//奖分周数据
                'list_buckle'=>$list_buckle,//扣分周数据
            ];
        }
        return $data;
    }
}
